import { WeaLocaleProvider } from 'ecCom';
import { message } from 'antd';

const getLabel = WeaLocaleProvider.getLabel;

export const openWindow = (url) => {
  const width = screen.availWidth - 10;
  const height = screen.availHeight - 50;
  let szFeatures = 'top=0,';
  szFeatures += 'left=0,';
  szFeatures += `width=${width},`;
  szFeatures += `height=${height},`;
  szFeatures += 'directories=no,';
  szFeatures += 'status=yes,toolbar=no,location=no,';
  szFeatures += 'menubar=no,';
  szFeatures += 'scrollbars=yes,';
  szFeatures += 'resizable=yes';
  window.open(url, '', szFeatures);
};


export const loopTree = (datas, key) => datas.map((data) => {
  if (data.domid === key) return { ...data, isSelect: true, childs: [] };
  if (data.childs && data.childs.length > 0) {
    const loop = loopTree(data.childs, key);
    const childs = loop.filter(c => c.isSelect === true);
    return { ...data, isSelect: childs.length > 0, childs };
  }
  return { ...data };
}).filter(d => d.isSelect === true);

const getPathArr = (datas, arr = []) => {
  datas.forEach((l) => {
    arr.push({ ...l, childs: [] });
    if (l.childs && l.childs.length > 0) {
      getPathArr(l.childs, arr);
    }
  });
  return arr;
};

export const getPathOfTree = (datas = [], key = '') => getPathArr(loopTree(datas, key));
export const catchApiError = result => new Promise((resolve) => {
    if (!result.api_status) {
      message.error(result.msg || getLabel(30651, '操作失败'));
      return;
    }
    resolve(result);
  });

export const GetUrlParam = (url, paraName) => {
  let arrObj = url.split('?');
  if (arrObj.length > 1) {
      let arrPara = arrObj[1].split('&');
      let arr;
      for (let i = 0; i < arrPara.length; i++) {
          arr = arrPara[i].split('=');
          if (arr !== null && arr[0] === paraName) {
              return arr[1];
          }
      }
      return '';
  }
      return '';
};

